package top.szhome.system.controller.login;

import com.alibaba.druid.support.json.JSONUtils;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
import top.szhome.framework.core.tools.exception.SzhomeException;
import top.szhome.framework.core.tools.support.Resp;
import top.szhome.framework.core.tools.util.WebUtil;
import top.szhome.framework.starter.boot.annotation.DisabledAdvice;
import top.szhome.system.dto.LoginDTO;
import top.szhome.system.dto.UserDTO;
import top.szhome.system.service.UserService;
import top.szhome.system.vo.UserVO;

import java.util.HashMap;
import java.util.Map;

/**
 * @author 唐加利
 * @date 2023/7/19
 **/
@RequiredArgsConstructor
@RestController
public class LoginController {

    private final UserService userService;

    @GetMapping("currentUser")
    @DisabledAdvice
    public Resp<Object> currentUser() {
        UserVO user = (UserVO) WebUtil.getRequest().getSession().getAttribute("current");
        if(user == null){
            return Resp.fail("未登录");
        }
        String json = "{\n" +
                "        \"name\": \"" + user.getRealName() + "\",\n" +
                "        \"avatar\": \"https://gw.alipayobjects.com/zos/antfincdn/XAosXuNZyF/BiazfanxmamNRoxxVxka.png\",\n" +
                "        \"userid\": \"00000001\",\n" +
                "        \"email\": \"" + user.getEmail() + "\",\n" +
                "        \"signature\": \"海纳百川，有容乃大\",\n" +
                "        \"title\": \"交互专家\",\n" +
                "        \"group\": \"蚂蚁金服－某某某事业群－某某平台部－某某技术部－UED\",\n" +
                "        \"tags\": [\n" +
                "            {\n" +
                "                \"key\": \"0\",\n" +
                "                \"label\": \"很有想法的\"\n" +
                "            },\n" +
                "            {\n" +
                "                \"key\": \"1\",\n" +
                "                \"label\": \"专注设计\"\n" +
                "            },\n" +
                "            {\n" +
                "                \"key\": \"2\",\n" +
                "                \"label\": \"辣~\"\n" +
                "            },\n" +
                "            {\n" +
                "                \"key\": \"3\",\n" +
                "                \"label\": \"大长腿\"\n" +
                "            },\n" +
                "            {\n" +
                "                \"key\": \"4\",\n" +
                "                \"label\": \"川妹子\"\n" +
                "            },\n" +
                "            {\n" +
                "                \"key\": \"5\",\n" +
                "                \"label\": \"海纳百川\"\n" +
                "            }\n" +
                "        ],\n" +
                "        \"notifyCount\": 12,\n" +
                "        \"unreadCount\": 11,\n" +
                "        \"country\": \"China\",\n" +
                "        \"access\": \"admin\",\n" +
                "        \"geographic\": {\n" +
                "            \"province\": {\n" +
                "                \"label\": \"浙江省\",\n" +
                "                \"key\": \"330000\"\n" +
                "            },\n" +
                "            \"city\": {\n" +
                "                \"label\": \"杭州市\",\n" +
                "                \"key\": \"330100\"\n" +
                "            }\n" +
                "        },\n" +
                "        \"address\": \"西湖区工专路 77 号\",\n" +
                "        \"phone\": \"" + user.getPhone() + "\"\n" +
                "    }";
        return Resp.success(JSONUtils.parse(json));
    }

    @PostMapping("login/account")
    @DisabledAdvice
    public Map<String, Object> login(@RequestBody LoginDTO request) {
        UserVO user = userService.login(request.getUsername(), request.getPassword());
        if(user == null){
            throw new SzhomeException("登录失败，用户名或密码错误");
        }
        WebUtil.getRequest().getSession().setAttribute("current", user);

        Map<String, Object> result = new HashMap<>(16);
        result.put("status", "ok");
        result.put("type", "account");
        result.put("currentAuthority", "admin");
        return result;
    }

    @PostMapping("login/outLogin")
    @DisabledAdvice
    public Resp logout() {
        return Resp.success();
    }

}
